package br.com.baco.evento.gui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import br.com.baco.R;
import br.com.baco.evento.EventoFactory;
import br.com.baco.evento.dominio.Evento;
import br.com.baco.evento.negocio.IEventoServices;
import br.com.baco.gui.InicialActivity;

public class ListarEventosActivity extends ActionBarActivity {
	private IEventoServices eventoServices = EventoFactory.getInstance().createServices(this);
	ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_listar_eventos);
		
		this.Events();
		
        List<String> listaEventosNome = getEventoName();
        
        ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, listaEventosNome);
        final ListView listView = (ListView) findViewById(R.id.lista_evento_listar_eventos2);
        listView.setAdapter(ad);
        
        listView.setOnItemClickListener( new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				Intent intent = new Intent(ListarEventosActivity.this, VisualizarEventoActivity.class);
				
				String  itemValue    = (String) listView.getItemAtPosition(position);
				String[] splitItemValue = itemValue.split("-");
				
                intent.putExtra("id", splitItemValue[0]);
                
                startActivity(intent);
			}
		});
        
	}
 
    private List<String> getEventoName() {
        List<String> listaNomeEvento = new ArrayList<String>();
        List<Evento> listaEventos = eventoServices.listar();
        System.out.println(eventoServices.listar());
        for (Evento evento : listaEventos) {
			//listaNomeEvento.add(evento.getNome());
        	listaNomeEvento.add(evento.getId()+"-"+evento.getNome());
		}
        return listaNomeEvento;				
	}
	public void Events(){

		//Bot�o Configura��es
		Button btnNovo = (Button) findViewById(R.id.botao_evento_listar_novo2) ;
		//Direcionar para activity eventos
		btnNovo.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View v) {
				 Intent intent = new Intent();
	             intent.setClass(ListarEventosActivity.this, AdicionarEventoActivity.class);
	             startActivity(intent);
	             finish();
	             overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.listar_eventos, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	public void onBackPressed() {
	    // do something on back.
		Intent back = new Intent(this, InicialActivity.class);
		startActivity(back);
        finish();
	    
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.activity_listar_eventos,
					container, false);
			return rootView;
		}
	}

}
